﻿/*
 *  Author: Louis A. Leskoviansky
 *  Company: Artemis Solutions Group (http://www.artemis-solutions.com)
 */


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Compilation;
using System.Web.UI;

using AjaxControlFramework.Compilation;

namespace AjaxControlFramework
{
    public class CompiledScript : BaseScriptControl
    {
        //------// Properties \\--------------------------------------------\\
        [Bindable(false)]
        [Browsable(false)]
        public override string Name
        {
            get { return base.Name; }
            set { base.Name = value; }
        }


        [Bindable(false)]
        [Browsable(false)]
        public override ScriptType Type
        {
            get { return base.Type; }
            set { base.Type = value; }
        }


        private string _src = String.Empty; // default value
        public virtual string Src
        {
            get { return _src; }
            set
            {
                if (value == null)
                {
                    _src = String.Empty;
                }
                else
                {
                    _src = value;
                }
            }
        }
        //------\\ Properties //--------------------------------------------//



        //------// Constructors \\------------------------------------------\\
        public CompiledScript()
            : base()
        { }
        //------\\ Constructors //------------------------------------------//



        //------// Methods \\-----------------------------------------------\\
        public override void ValidateProperties()
        {
            
        }


        public override void RenderControl(HtmlTextWriter writer)
        {
            DataBind();

            if (InternalScriptManager.Instance.RegisteredScripts.ContainsKey(Name))
            {
                ScriptReference scriptReference = InternalScriptManager.Instance.RegisteredScripts[Name];

                if (!scriptReference.Rendered && scriptReference.Enabled)
                {
                    writer.Write(scriptReference.GenerateScriptBlock());

                    InternalScriptManager.Instance.RenderedScripts.Add(Name, scriptReference);
                }
            }
        }


        public override ScriptReference GenerateScriptReference()
        {
            JsxScript script = JsxScriptRegistry.GetScript(Name);

            if (script == null)
            {
                // If the .jsx script cannot be found in the JsxScriptRegistry then it is either inheriting 
                // a non-template control and hasn't been registered yet, is a singleton script and hasn't 
                // been registered yet, or is not a valid .jsx file.

            }

            if (script == null) // If script is stiiiill null...
            {
                return null;
            }

            base.Name = script.ScriptName;
            base.Type = script.ScriptType;
            base.Enabled = script.Enabled;

            ScriptReference scriptReference = new CompiledScriptReference(base.Name, Src, base.Type, base.Enabled);
            scriptReference.Dependencies = new List<string>(Dependencies);

            return scriptReference;
        }
        //------\\ Methods //-----------------------------------------------//
    }
}
